<template>
	<view class="complete_page">
		<Search @handleInput="handleInput"></Search>
		<scroll-view class="scroll_view" scroll-y>
			<view @click="clickItem(item)" class="scroll_view_item" v-for="(item, i) in showList" :key="i">
				<text class="item_ch">{{item.chinese}}</text>
				<text class="item_en" v-if="item.english1">{{item.english1}}</text>
				<text class="item_en" v-if="item.english2">{{item.english2}}</text>
				
				<text class="item_lose" v-if="!checkComplete(item)">待补充</text>
			</view>
		</scroll-view>
		<button class="back_bottom" type="default" @click="goBack">返 回</button>
	</view>
</template>

<script>
	import 	Search from '../../components/search_bar.vue'
	export default {
		components:{
			Search
		},
		data() {
			return {
				allList: [],
				showList: []
			}
		},
		onShow () {
			try {
			    this.allList = uni.getStorageSync('words') || []
				this.showList = JSON.parse(JSON.stringify(this.allList))
			} catch (e) {
			    console.log('获取缓存出错', e)
			}
		},
		methods: {
			// 点击单词
			clickItem (item) {
				if (this.checkComplete(item)) { // 去查看
					this.checkWord(item)
				} else { // 去补充
					this.updateWord(item)
				}
			},
			// 修改
			updateWord (item) {
				try {
				    uni.setStorageSync('update', item);
					setTimeout(() => {
						uni.navigateTo({
						    url: '../form/form',
							animationType: 'pop-in',
							animationDuration: 300
						});
					}, 0)
				} catch (e) {}
			},
			// 查看
			checkWord (item) {
				try {
				    uni.setStorageSync('curr', item);
					setTimeout(() => {
						this.goBack()
					}, 0)
				} catch (e) {}
			},
			// 搜索
			handleInput (e) {
				if (!e) {
					this.showList = JSON.parse(JSON.stringify(this.allList))
					return
				}
				let arr = []
				this.allList.forEach(w => {
					if (
						w.chinese.indexOf(e) > -1
						|| w.english1.indexOf(e) > -1
						|| w.english2.indexOf(e) > -1
					) {
						arr.push(w)
					}
				})
				this.showList = arr
			},
			// 检测完整性
			checkComplete (item) {
				return  item.english1 
						&& item.english2
						&& item.eng1_eg1_cn
						&& item.eng1_eg1_en
						&& item.eng1_eg2_cn
						&& item.eng1_eg2_en
						&& item.eng1_eg3_cn
						&& item.eng1_eg3_en
						&& item.eng2_eg1_cn
						&& item.eng2_eg1_en
						&& item.eng2_eg2_cn
						&& item.eng2_eg2_en
						&& item.eng2_eg3_cn
						&& item.eng2_eg3_en
			},
			// 返回上一页
			goBack () {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped>
	.complete_page {
		width:100%;
		height:100%;
		background-color: #222;
		overflow: hidden;
		position: relative;
		color: #ddd;
		font-size: 36rpx;
		box-sizing: border-box;
		padding-top: 40rpx;
	}
	.back_bottom {
		position: absolute;
		width: 90%;
		left: 5%;
		color: #999;
		background-color: rgba(0,0,0,0);
		bottom: 20rpx;
	}
	.scroll_view {
		width: 90%;
		height: calc(100% - 200rpx);
		margin: 20rpx auto;
	}
	.scroll_view_item {
		width: 100%;
		height: 100rpx;
		line-height: 100rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		margin: 30rpx 0;
		background-color: rgba(254,254,254,0.2);
		display: flex;
		flex-direction: row;
		color: #fff;
		align-items: center;
		position: relative;
		border-radius: 12rpx;
	}
	.item_ch {
		font-size: 30rpx;
		background-color: #93b5cf;
		color: #000;
		height: 60rpx;
		line-height: 60rpx;
		padding: 0rpx 30rpx;
		width: 150rpx;
		border-radius: 24rpx;
		font-weight: bold;
	}
	.item_en {
		background-color: rgba(192,44,56,0.5);
		font-size: 26rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 20rpx;
		margin-left: 20rpx;
		border-radius: 24rpx;
		color: #dedede;
	}
	.item_lose {
		position: absolute;
		right: 20rpx;
		top: -26rpx;
		font-size: 24rpx;
		color: #f1ca17;
	}
</style>
